package org.takinframework.web.system.service.impl;

import java.io.Serializable;
import java.net.SocketException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.takinframework.core.common.service.impl.BaseServiceImpl;
import org.takinframework.core.util.BrowserUtils;
import org.takinframework.core.util.ContextHolderUtils;
import org.takinframework.core.util.Page;
import org.takinframework.core.util.StringUtil;
import org.takinframework.core.util.oConvertUtils;
import org.takinframework.web.system.entity.SysFunction;
import org.takinframework.web.system.entity.SysLog;
import org.takinframework.web.system.entity.SysType;
import org.takinframework.web.system.entity.SysTypeGroup;
import org.takinframework.web.system.service.SystemService;
import org.takinframework.web.util.WebUtil;

@Service("systemService")
@Transactional
public class SystemServiceImpl extends BaseServiceImpl implements SystemService {
	

	/**
	 * 添加日志
	 * @throws SocketException 
	 * @throws SQLException 
	 */
	public void addLog(String logcontent, Short operatetype, Short loglevel){
		addLog(logcontent, operatetype, loglevel, null);
	}
	
	public void addLog(String LogContent, Short operatetype, Short loglevel,
			Exception e) {
		
		HttpServletRequest request = ContextHolderUtils.getRequest();
		String broswer = BrowserUtils.checkBrowse(request);
		SysLog log = new SysLog();
		log.setParams(WebUtil.getParams(request));
		log.setLogLevel(loglevel);
		log.setIp(oConvertUtils.getIp());
		log.setUserAgent(broswer);
		log.setType(operatetype);
		log.setRequestUrl(request.getRequestURI());
		log.setMethod(request.getMethod());
		log.setContent(LogContent);
		if(StringUtil.isNotEmpty(e)){
			log.setException(e.getMessage());
		}
		
		try {
			jdbcDao.save(log);
		} catch (SQLException ex) {
		}
		
	}
	


	/**
	 * 根据类型编码和类型名称获取Type,如果为空则创建一个
	 * 
	 * @param typecode
	 * @param typename
	 * @return
	 */
	public <T> T getType(String typecode, String typename, SysTypeGroup sysTypeGroup) {
//		SysType actType = commonDao.findUniqueByProperty(SysType.class, "typecode", typecode);
//		if (actType == null) {
//			actType = new TSType();
//			actType.setTypecode(typecode);
//			actType.setTypename(typename);
//			actType.setTSTypegroup(tsTypegroup);
//			commonDao.save(actType);
//		}
//		return actType;
		return null;

	}

	/**
	 * 根据类型分组编码和名称获取TypeGroup,如果为空则创建一个
	 * 
	 * @param typecode
	 * @param typename
	 * @return
	 */
	public <T> T getTypeGroup(String typegroupcode, String typgroupename) {
//		SysTypeGroup tsTypegroup = commonDao.findUniqueByProperty(SysTypeGroup.class, "typegroupcode", typegroupcode);
//		if (tsTypegroup == null) {
//			tsTypegroup = new SysTypeGroup();
//			tsTypegroup.setTypegroupcode(typegroupcode);
//			tsTypegroup.setTypegroupname(typgroupename);
//			commonDao.save(tsTypegroup);
//		}
//		return tsTypegroup;
		return null;
	}

	
	public <T> T getTypeGroupByCode(String typegroupCode) {
//		SysTypeGroup tsTypegroup = commonDao.findUniqueByProperty(SysTypeGroup.class, "typegroupcode", typegroupCode);
//		return tsTypegroup;
		return null;
	}

	
	public void initAllTypeGroups() {
//		List<SysTypeGroup> typeGroups = this.jdbcDao.findAll(SysTypeGroup.class,"methord1");
//		for (SysTypeGroup tsTypegroup : typeGroups) {
//			SysTypeGroup.allTypeGroups.put(tsTypegroup.getTypeGroupCode().toLowerCase(), tsTypegroup);
//			List<SysType> types = this.jdbcDao.findList(SysType.class,"methord1", tsTypegroup.getId());
//			SysTypeGroup.allTypes.put(tsTypegroup.getTypeGroupCode().toLowerCase(), types);
//		}
	}

	
	public void refleshTypesCach(SysType type) throws SQLException {
//		SysTypeGroup tsTypegroup = this.jdbcDao.getEntity(SysTypeGroup.class,"methord1", type.getTypeGroupId());
//		List<SysType> types = this.jdbcDao.findList(SysType.class,"methord1", tsTypegroup.getId());
//		SysTypeGroup.allTypes.put(tsTypegroup.getTypeGroupCode().toLowerCase(), types);
	}

	
	public void refleshTypeGroupCach() {
//		TSTypegroup.allTypeGroups.clear();
//		List<TSTypegroup> typeGroups = this.commonDao.loadAll(TSTypegroup.class);
//		for (TSTypegroup tsTypegroup : typeGroups) {
//			TSTypegroup.allTypeGroups.put(tsTypegroup.getTypegroupcode().toLowerCase(), tsTypegroup);
//		}
	}

	// ----------------------------------------------------------------
	// ----------------------------------------------------------------

	
	public <T> Set<T> getOperationCodesByRoleIdAndFunctionId(String roleId, String functionId) {
		Set<T> operationCodes = new HashSet<T>();
//		TSRole role = commonDao.get(TSRole.class, roleId);
////		CriteriaQuery cq1 = new CriteriaQuery(TSRoleFunction.class);
//		cq1.eq("TSRole.id", role.getId());
//		cq1.eq("TSFunction.id", functionId);
//		cq1.add();
//		List<TSRoleFunction> rFunctions = getListByCriteriaQuery(cq1, false);
//		if (null != rFunctions && rFunctions.size() > 0) {
//			TSRoleFunction tsRoleFunction = rFunctions.get(0);
//			if (null != tsRoleFunction.getOperation()) {
//				String[] operationArry = tsRoleFunction.getOperation().split(",");
//				for (int i = 0; i < operationArry.length; i++) {
//					operationCodes.add(operationArry[i]);
//				}
//			}
//		}
		return operationCodes;
	}

	
	public <T> Set<T> getOperationCodesByUserIdAndFunctionId(String userId, String functionId) {
		Set<T> operationCodes = new HashSet<T>();
//		List<TSRoleUser> rUsers = findByProperty(TSRoleUser.class, "TSUser.id", userId);
//		for (TSRoleUser ru : rUsers) {
//			TSRole role = ru.getTSRole();
//			CriteriaQuery cq1 = new CriteriaQuery(TSRoleFunction.class);
//			cq1.eq("TSRole.id", role.getId());
//			cq1.eq("TSFunction.id", functionId);
//			cq1.add();
//			List<TSRoleFunction> rFunctions = getListByCriteriaQuery(cq1, false);
//			if (null != rFunctions && rFunctions.size() > 0) {
//				TSRoleFunction tsRoleFunction = rFunctions.get(0);
//				if (null != tsRoleFunction.getOperation()) {
//					String[] operationArry = tsRoleFunction.getOperation().split(",");
//					for (int i = 0; i < operationArry.length; i++) {
//						operationCodes.add(operationArry[i]);
//					}
//				}
//			}
//		}
		return operationCodes;
	}

	// ----------------------------------------------------------------
	// ----------------------------------------------------------------
	
	public void flushRoleFunciton(String id, SysFunction newFunction) {
//		TSFunction functionEntity = this.getEntity(TSFunction.class, id);
//		if (functionEntity.getTSIcon() == null || !StringUtil.isNotEmpty(functionEntity.getTSIcon().getId())) {
//			return;
//		}
//		TSIcon oldIcon = this.getEntity(TSIcon.class, functionEntity.getTSIcon().getId());
//		if (!oldIcon.getIconClas().equals(newFunction.getTSIcon().getIconClas())) {
//			// 刷新缓存
//			HttpSession session = ContextHolderUtils.getSession();
//			TSUser user = ResourceUtil.getSessionUserName();
//			List<TSRoleUser> rUsers = this.findByProperty(TSRoleUser.class, "TSUser.id", user.getId());
//			for (TSRoleUser ru : rUsers) {
//				TSRole role = ru.getTSRole();
//				session.removeAttribute(role.getId());
//			}
//		}
	}

	public <T> T getTypeGroupByCode(Class<T> entity) {
		return null;
	}

	public <T> void refleshTypesCach(Class<T> entity) {
		
	}

	public <T> T checkEntityExits(Class<T> entity, String methord)
			throws SQLException {
		return jdbcDao.checkEntityExits(entity, methord);
	}

	public <T> T checkEntityExits(T entity) throws Exception {
		return jdbcDao.checkEntityExits(entity);
	}

	public <T> T checkEntityExits(T entity, Object... param) throws Exception {
		return jdbcDao.checkEntityExits(entity, param);
	}

	public <T> T getEntity(Class<T> entity, String methord, Serializable id)
			throws SQLException {
		return jdbcDao.getEntity(entity, methord, id);
	}

	public <T> Integer getEntityCount(Class<T> entity, String methord,
			Serializable id) {
		return jdbcDao.getEntityCount(entity, methord, id);
	}

	public <T> Integer getEntityCount(T entity) throws Exception {
		return jdbcDao.getEntityCount(entity);
	}

	public <T> Integer getEntityCount(T entity, Object... param)
			throws Exception {
		return jdbcDao.getEntityCount(entity, param);
	}

	public <T> Integer getEntityCountById(T entity) throws Exception {
		return jdbcDao.getEntityCountById(entity);
	}

	public <T> List<T> findList(Class<T> entity, String methord, Serializable id) {
		return jdbcDao.findList(entity, methord, id);
	}

	public <T> List<T> findList(Class<T> entity, String methord,
			Object... param) {
		return jdbcDao.findList(entity, methord, param);
	}

	public <T> List<T> findList(T entity) throws Exception {
		return jdbcDao.findList(entity);
	}

	public <T> List<T> findList(T entity, Object... param) throws Exception {
		return jdbcDao.findList(entity, param);
	}

	public <T> List<T> findListByPage(T entity, Object... param)
			throws Exception {
		return jdbcDao.findListByPage(entity, param);
	}

	public <T> List<T> findAll(Class<T> entity, String methord) {
		return jdbcDao.findAll(entity, methord);
	}

	public <T> List<T> findAll(Class<T> entity) throws SQLException {
		return jdbcDao.findAll(entity);
	}

	public <T> List<T> findAllByPage(T entity) throws Exception {
		return jdbcDao.findAllByPage(entity);
	}
	
	public <T> Page<T> findAllByPage(Page<T> page, T entity) throws Exception {
		return jdbcDao.findAllByPage(page, entity);
	}

	public void executeSql(String sql, Object... param) {
		jdbcDao.executeSql(sql, param);
	}

	public <T> void delete(T entity) {
		jdbcDao.delete(entity);
	}

	public <T> void delete(T entity, Object... params) {
		jdbcDao.delete(entity, params);
	}

	public <T> void save(T entity) throws SQLException {
		jdbcDao.save(entity);
	}

	public <T> void batchSave(List<T> entitys) throws SQLException {
		jdbcDao.batchSave(entitys);
	}

	public <T> void update(T entity) throws Exception {
		jdbcDao.update(entity);
	}

	public <T> String getDBType() throws SQLException {
		return jdbcDao.getDBType();
	}

	public <T> void deleteAllEntitie(Collection<T> entities) {
		jdbcDao.delete(entities);
	}
	
}
